package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.InformationBean
import com.example.presentshopping.config.bean.SupplierListBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_information.*
import kotlinx.android.synthetic.main.activity_payby_alipay.*
import kotlinx.android.synthetic.main.activity_payby_alipay.title_main_tv

class InformationActivity : BaseMvpActivity() {

    var informationBean :  InformationBean?=null

    private var queryInfoPresenter: PublicPresenter? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<InformationBean.DataBean>? = null
    var page = 1
    override val layoutId: Int
        get() = R.layout.activity_information

    override fun initView(savedInstanceState: Bundle?) {
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)
        setAdapter()
        RgetGoodsList()


    }

     fun  setAdapter(){
         baseRecyclerAdapter = object :BaseRecyclerAdapter<InformationBean.DataBean>(this,null,R.layout.item_information){
             override fun convert(
                 holder: BaseRecyclerHolder?,
                 data: InformationBean.DataBean,
                 position: Int,
                 isScrolling: Boolean,
                 selectedPosition: Int
             ) {
               val iv_infor = holder?.getView<RoundImageView>(R.id.iv_infor)
               val tel_tv  =holder?.getView<TextView>(R.id.tel_tv)
                   val  liv_infor = holder?.getView<LinearLayout>(R.id.liv_infor)

                 GlideUtil.load(context,data.img,iv_infor)
                 tel_tv?.text =data.title

                 liv_infor?.setOnClickListener {
                     val iniet = Intent(context, TurntableActivity().javaClass)
                iniet.putExtra("Activity","InformationActivity")
                    .putExtra("url",data.url)
                    .putExtra("title",data.title)
                startActivity(iniet)
                 }
             }

         }
         recycler_view.adapter = baseRecyclerAdapter

     }


    fun RgetGoodsList(){
        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_getInformation,HttpRequest.getGoodsDetail(""))
    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }




    override fun showToast(msg: String?) {
     ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        informationBean =GsonUtils.getGson().fromJson(data,InformationBean().javaClass)
        baseRecyclerAdapter?.setData(informationBean?.data)
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }


}